var _max = 0;
function init(){
	_max = get_max_line(400,25);
};
function invoice_type(){
	var is = $('#type').val();
	is==11||is==21?$('#is_sell').show():$('#is_sell').hide();
};
var _max_length = 0;
var _sn = 0;
var _merAmount = new Array();
function add_merchandise(id){
	var url = 'merchandise_list.html?'+id;
	var config = 'dialogWidth:810px;dialogHeight:565px;scroll:no;status:no;';
//	var merchandise_list = window.open(url,'',config);
	var merchandise_list = window.showModalDialog(url,'',config);
	merchandise_list = merchandise_list==null?'':merchandise_list;
	//=========================
	// 删除之前存在的 foot 
	var foot = document.getElementById('foot');
	if(null!=foot&&merchandise_list.length>0){
		foot.removeNode(true);
	}
	//=========================
	_max_length =_max_length - merchandise_list.length;
	var is_less = 0-_max_length<=_max;
	for(var i=0;i<merchandise_list.length;i++){
		var merchandise_info = merchandise_list[i].split('?');
		var merchandise_list_html = '';
		var style_border1 = !is_less&&i==merchandise_list.length-1?'border-bottom:0;':'';
		var style_border2 = i==0&&_sn>0?' border-top:1px solid #888;':'';
		var style_border = 'style="' + style_border1 + style_border2 + '"';
		merchandise_list_html = merchandise_list_html + '<tr id="info_' + _sn + '" style="height:25px;" onmouseover="cbgcolor(this,0)" onmouseout="cbgcolor(this,1)">';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="200"><input type="hidden" name="merchandise_id" sn="' + _sn + '" value="' + merchandise_info[0] + '" />' + merchandise_info[1] + '</td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="">' + merchandise_info[2] + '</td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="100"><input type="test" name="merchandise_price" id="merchandise_price_' + _sn + '" sn="' + _sn + '" value="0" onpropertychange="get_total(this)" style="width:94px; text-align:center;"  /></td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="100"><input type="test" name="merchandise_number" id="merchandise_number_' + _sn + '" sn="' + _sn + '" value="'+merchandise_info[4]+'" onpropertychange="get_total(this)" style="width:94px; text-align:center;" /></td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="50">' + merchandise_info[3] + '</td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="100"><input type="test" name="merchandise_total" id="merchandise_total_' + _sn + '" value="0" onpropertychange="get_all_total()"  style="width:94px; text-align:center;" readonly="readOnly"  /></td>';
		merchandise_list_html = merchandise_list_html + '  <td class="list" ' + style_border + ' width="40"><input type="button" onclick="del_merchandise(this)" value="删" /></td>';
		merchandise_list_html = merchandise_list_html + '</tr>';
		_merAmount[i] = merchandise_info[4];
		_sn++;
		$('#merchandise_list').append(merchandise_list_html);
	}
	if(merchandise_list.length>0){
		add_foot(is_less);
	}
};
function add_foot(is_less){
	var foot = document.getElementById('foot');
	if(is_less&&null==foot){
		var merchandise_list_html = '';
		merchandise_list_html = merchandise_list_html + '<tr id="foot">';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="200">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="100">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="100">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="50">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="100">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '  <td style="border-bottom:0; border-top:1px solid #888;" class="list" width="40">&nbsp;</td>';
		merchandise_list_html = merchandise_list_html + '</tr>';
		$('#merchandise_list').append(merchandise_list_html);
	}
};
function del_merchandise(obj){
	var container = obj.parentNode.parentNode;
	container.removeNode(true);
	_max_length++;
	var is_less = 0-_max_length<=_max;
	add_foot(is_less);
	get_all_total();
};
function get_total(obj){
	var sn = obj.sn;
	var price = $('#merchandise_price_'+sn).val();
	var number = $('#merchandise_number_'+sn).val();
	var total = price * number;
	$('#merchandise_total_'+sn).val(total);
};
function get_all_total(){
	var totals = document.getElementsByName('merchandise_total');
	var all_total = 0;
	for(var i=0;i<totals.length;i++){
		all_total = all_total - totals[i].value;
	}
	$('#invoice_total').val(0 - all_total);
};
function selectSupplier(){
	var _type = '';
	var type = $('#type').val();
	invoice_type();
	
	// =11  查询  供应商（0）   =21 查询  客户（1）
	if(type==11){
		_type = 0;
		findSupplierByTpye(_type);
	}else if(type==21){
		_type = 1;
		findSupplierByTpye(_type);
	}else{
		$('#supplier').html('');
	}
};
function findSupplierByTpye(_type){
	
	var url = '/storage/manager/pages/invoice.do?method=selectSupplier&d=' + new Date();
	url = url + '&tpye=' + _type;

	$.getJSON(url, {}, function(data){
		var supplierList = '';
		for(var i=0;i<data.length;i++){
			supplierList = supplierList + '<option value="'+data[i][0]+'" >'+data[i][1]+'</option>';
		}
		$('#supplier').html(supplierList);
	});
};
function selectWarehouse(){
	var url = '/storage/manager/pages/invoice.do?method=selectWarehouse&d=' + new Date();
	
	$.getJSON(url, {}, function(data){
		var warehouseList = '';
		for(var i=0;i<data.length;i++){
			warehouseList = warehouseList + '<option value="'+data[i][0]+'" >'+data[i][1]+'</option>';
		}
		$('#warehouse').html(warehouseList);
	});
};
function selectAttn(){
	var url = '/storage/manager/pages/invoice.do?method=selectAttn&d=' + new Date();
	
	$.getJSON(url, {}, function(data){
		var attnList = '';
		for(var i=0;i<data.length;i++){
			attnList = attnList + '<option value="'+data[i][0]+'" >'+data[i][1]+'</option>';
		}
		$('#attn').html(attnList);
	});
};
function saveInvoice(){
	var type = $('#type').val();
	var invoiceCid = $('#invoiceCid').val();
	var supplier = $('#supplier').val();
	var warehouse = $('#warehouse').val();
	var arrival_date = $('#arrival_date').val();
	var attn = $('#attn').val();
	var memo = $('#memo').val();
	var invoice_total = $('#invoice_total').val();
	
	var merchandise_items = document.getElementsByName('merchandise_id');
	var merchandise_ids = new Array();
	var merchandise_prices = new Array();
	var merchandise_numbers = new Array();
	var merchandise_totals = new Array();
	
	var is_Adequate = true;
	var is_less = true;
	for(var i=0;i<merchandise_items.length;i++){
		merchandise_ids[i] = merchandise_items[i].value;
		merchandise_prices[i] = $('#merchandise_price_'+merchandise_items[i].sn).val();
		merchandise_numbers[i] = $('#merchandise_number_'+merchandise_items[i].sn).val();
		merchandise_totals[i] = $('#merchandise_total_'+merchandise_items[i].sn).val();
		is_Adequate = _merAmount[i] >= merchandise_numbers[i];
		if(!is_Adequate&&('21'==type||'22'==type)){
			document.getElementById('info_'+merchandise_items[i].sn).style.color = 'red';
			$('#merchandise_number_'+merchandise_items[i].sn).val(merchandise_numbers[i]+' / '+_merAmount[i]);
			is_less = false;
		}
	}
	
	var isFull = false;
	if('11'==type||'21'==type){
		isFull = merchandise_items.length>0 && ''!=invoiceCid && ''!=arrival_date && 
				 ''!=attn && ('21'==type?true:''!=warehouse) && ''!=supplier;
	}else if('12'==type||'22'==type){
		isFull = merchandise_items.length>0 && ''!=invoiceCid && ''!=arrival_date && 
				 ''!=attn && ('22'==type?true:''!=warehouse) ;
	}

	
	if(isFull){
		if(is_less){
			var url = '/storage/manager/pages/invoice.do?method=create&d=' + new Date();
		
			url = url + '&type=' + escape(escape(type));
			url = url + '&invoiceCid=' + escape(escape(invoiceCid));
			url = url + '&supplier=' + escape(escape(supplier));
			url = url + '&warehouse=' + escape(escape(warehouse));
			url = url + '&arrival_date=' + escape(escape(arrival_date));
			url = url + '&attn=' + escape(escape(attn));
			url = url + '&memo=' + escape(escape(memo));
			url = url + '&merchandise_ids=' + escape(escape(merchandise_ids));
			url = url + '&merchandise_prices=' + escape(escape(merchandise_prices));
			url = url + '&merchandise_numbers=' + escape(escape(merchandise_numbers));
			url = url + '&merchandise_totals=' + escape(escape(merchandise_totals));
			url = url + '&invoice_total=' + escape(escape(invoice_total));
			
			$.getJSON(url, {}, function(data){
				if(data=="true"){
					alert("保存成功");
					window.location.reload();
				}else{
					alert("保存失败");
				}
			});
		}else{
			alert('物品数量不足，请修正');
		}
	}else{
		alert('必要信息不完整');
	}
};